#include "bsp_uart.h"
#include "bsp_nvs.h"

static const char *TAG = "uart";
static QueueHandle_t uart0_queue;

void uart_event_task(void *pvParameters)
{
    uint8_t ret = NULL;
    uart_event_t event;
    size_t buffered_size;
    uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
    uart_ble_info_t uart_ble_info = {0};
    nvs_info_t *uart_device_id_info = get_nvs_info();
    for(;;) {
        //Waiting for UART event.
        if(xQueueReceive(uart0_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
            bzero(dtmp, RD_BUF_SIZE);                       // 字符串前N个数据擦除
            /* 串口数据接收 */
            if(UART_DATA == event.type){
                uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);
                switch(uart_ble_info.uart_ble_info_process){
                    case UART_BLE_INFO_PROCESS_START:
                        if(memcmp(dtmp, "BLE", 3) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_1;
                            if((uart_device_id_info->device_id_flag) == DEVICE_ID_SET_FLAG){
                                printf("BLE_NAME:%s\n", uart_device_id_info->device_id);
                            }else{
                                printf("DEFAULT_BLE_NAME\n");
                            }
                            printf("1:SET_BLE_NAME;0:CANCEL\n");
                        }
                        break;
                    case UART_BLE_INFO_PROCESS_1:
                        if(memcmp(dtmp, "1", 1) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_2;
                            printf("INPUT NAME:\n");
                        }else if(memcmp(dtmp, "0", 1) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_START;
                            printf("CANCEL\n");
                        }
                        break;
                    case UART_BLE_INFO_PROCESS_2:
                        /* 直接获取dtmp字符串 */
                        uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_3;
                        memset(uart_ble_info.uart_ble_name_buffer, 0, sizeof(uart_ble_info.uart_ble_name_buffer));
                        memcpy(uart_ble_info.uart_ble_name_buffer, dtmp, event.size);
                        printf("BLE_NAME_NEW:%s\n", uart_ble_info.uart_ble_name_buffer);
                        printf("1:CONFIRM NAME;2:REMAKE NAME;0:CONCEL\n");
                        break;
                    case UART_BLE_INFO_PROCESS_3:
                        if(memcmp(dtmp, "1", 1) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_START;
                            memset(uart_device_id_info->device_id, 0, sizeof(uart_device_id_info->device_id));
                            memcpy(uart_device_id_info->device_id, uart_ble_info.uart_ble_name_buffer, sizeof(uart_ble_info.uart_ble_name_buffer));
                            uart_device_id_info->device_id[18] = '\0';
                            uart_device_id_info->device_id_flag = DEVICE_ID_SET_FLAG;
                            /* 存储蓝牙信息 */
                            save_device_id_nvs_custom();
                            printf("BLE NAME CAHNGED,NEED REBOOT!!\n");
                        }else if(memcmp(dtmp, "2", 1) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_2;
                            printf("INPUT REMAKE NAME:\n");
                        }else if(memcmp(dtmp, "0", 1) == 0){
                            uart_ble_info.uart_ble_info_process = UART_BLE_INFO_PROCESS_START;
                            printf("CONCEL\n");
                        }
                        break;
                    default:
                        break;
                }
            }


        }
    }
    free(dtmp);
    dtmp = NULL;
    vTaskDelete(NULL);
}


void my_uart0_init()
{
    esp_log_level_set(TAG, ESP_LOG_INFO);

    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    //Install UART driver, and get the queue.
    uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
    uart_param_config(EX_UART_NUM, &uart_config);
    //Set UART log level
    esp_log_level_set(TAG, ESP_LOG_INFO);
    //Set UART pins (using UART0 default pins ie no changes.)
    uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    //Set uart pattern detect function.
    uart_enable_pattern_det_baud_intr(EX_UART_NUM, '+', PATTERN_CHR_NUM, 9, 0, 0);
    //Reset the pattern queue length to record at most 20 pattern positions.
    uart_pattern_queue_reset(EX_UART_NUM, 20);
    //Create a task to handler UART event from ISR

}






